<!-- 推送提示demo -->
<template>
  <span @click="handleToPage" class="jump-route">{{ data.content }}</span>
  <audio ref="audioRef" autoplay></audio>
</template>
<script lang="ts" setup>
  import { ref, onMounted, onUnmounted } from "vue"
  import { propTypes } from "/@/utils/propTypes"
  import { useMessage } from "/@/hooks/web/useMessage"
  import { useRouter } from "vue-router"
  import { virtualToRealBaseUrl } from "/@/api/infra/file"
  const router = useRouter()
  const audioRef = ref<HTMLAudioElement>()
  const { createWarningModal, notification } = useMessage()
  const props = defineProps({
    data: propTypes.object,
  })

  onMounted(async () => {
    console.log("=================================> 组件加载了", props.data)
    const realUrl = await virtualToRealBaseUrl(props.data.mediaUrl)
    console.log("realUrl", realUrl)

    audioRef.value!.src = realUrl
    audioRef.value
      ?.play()
      .then(() => {
        console.log("可以自动播放")
      })
      .catch((err) => {
        console.log(err)
        // edge://settings/content/mediaAutoplay
        // chrome://settings/content/siteDetails
        createWarningModal({
          title: "当前浏览器不允许自动播放",
          width: 550,
          centered: false,
          content: `<div>
                  <span>Edge浏览器: 在 edge://settings/content/mediaAutoplay 中添加网站域名 </span> <br/>
                  <span>谷歌浏览器: 在 chrome://settings/content/siteDetails 中找到响应网站, 然后找到声音, 选择允许 </span>
                </div>`,
        })
        console.log("不允许自动播放")
      })
  })

  onUnmounted(() => {
    console.log("=================================> 组件卸载了", props.data)
  })

  const handleToPage = () => {
    console.log("jump route: ", props)
    const key = `${props.data.bizType}_${props.data.varData.orderId}`
    if (props.data.pageUrl?.startsWith("http")) {
      window.open(props.data.pageUrl, "_blank")
      notification.close(key)
      return
    }

    router.push({
      name: props.data.pageUrl,
      query: props.data.pageUrl,
    })
    notification.close(key)
  }
</script>

<style lang="less" scoped>
  span:hover {
    color: #1890ff;
    text-decoration: none;
    background-color: transparent;
    outline: none;
    cursor: pointer;
    transition: color 0.3s;
  }
</style>
